<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUIWXControl
 *
 * @author jianfeng
 */
class BUIWXControl extends BUIControl {

    const WeChat_Name = 'wechat';
    const WeXin_Name = 'wexin';

    protected $wxGroup;
    protected $wxMenu = array();

    public static function parseRequest() {
        $parsedData = parent::parseRequest();
        //$space = BWeChat::setContext();
        if (!empty($_GET['wxGroup']) && isset($_GET['wxGroup']) && $_GET['wxGroup'] != NULL) {
            $wxGroup = $_GET['wxGroup'];
            $wxData = array();
            $wxData['wxGroup'] = $wxGroup;

            if ($_GET['wxGroup'] == '97Sports') {
                $space = BSpace::find(array('name' => '97Sports'));
                if ($space instanceof BSpace) {
                    Blyn::app()->setActiveSpace($space);
                    $app = $space->getAppByName('bookingSports');
                    if ($app instanceof BApp)
                        Blyn::app()->setActiveApp($app);
                }
                if (isset($_GET['wxMenuItem']) && $_GET['wxMenuItem'] == 'bookingFacility') {
                    $operation = Blyn::app()->getActiveApp()->getOperation('bookingFacility');
                    if ($operation instanceof BOperation)
                        Blyn::app()->setActiveOperation($operation);
                }
                //this function will store current status into session
                Blyn::app()->getUIManager()->postStartRender();
            }
        }

        if (!empty($_GET['sp'])) {
            $spId = $_GET['sp'];
            $spaceId = $spId;
            if ($spaceId > 0) {
                $space = new BSpace($spaceId);
                Blyn::app()->setActiveSpace($space);
            }
        }

        if (!empty($_GET['op'])) {
            $opId = $_GET['op'];
            $operationId = $opId;
            if ($operationId > 0) {
                $operation = new BOperation($operationId);
                $app = $operation->getCurrentApp();
                Blyn::app()->setActiveApp($app);
                Blyn::app()->setActiveOperation($operation);
                Blyn::app()->setCurrentNode($operation);
            }
        }
        return $parsedData;
    }

    public function onParseRequest($data = NULL) {
        parent::onParseRequest($data);
    }

    public function getLinkUrl($name = null, $params = array(), $sender = null, $route = 'site/index', $data = array(), $ampersand = '&') {
        $data = array();
        if (is_array($params)) {
            $data = $params;
        }

        if (!is_array($params))
            $params = array();
        $spaceId = Blyn::app()->getActiveSpace()->getId();
        $params['dv'] = 'wx';
        $params['sp'] = $spaceId;
        $params['op'] = Blyn::app()->getActiveOperation()->getId();
        $params = $params + $data;

        return parent::getLinkUrl($name, $params, $sender, $route);
    }

    public function getAjaxLinkUrl($linkId, $updateControl = NULL, $data = array(), $route = 'site/index', $sender = null) {
        if (!is_array($data)) {
            $data = array();
        }
        $data['dv'] = 'wx';
        $data['sp'] = Blyn::app()->getActiveSpace()->getId();
        $data['op'] = Blyn::app()->getActiveOperation()->getId();
        return parent::getAjaxLinkUrl($linkId, $updateControl, $data, $route, $sender);
    }

    public function getAjaxLink($name, $data = array(), $sender = NULL, $route = 'site/index', $updateControl = NULL) {
        if (!is_array($data)) {
            $data = array();
        }
        $data['dv'] = 'wx';
        $data['sp'] = Blyn::app()->getActiveSpace()->getId();
        $data['op'] = Blyn::app()->getActiveOperation()->getId();
        return parent::getAjaxLink($name, $data, $sender, $route, $updateControl);
    }
    
    /**
     * this function use to parse wechat info such as userInfo
     */
    public static function parseWeChatInfo($data = NULL) {

        $appId = NULL;
        $appSecret = NULL;
        $parsedData = array();

        //$request = yii::app()->request;
        //if (isset($_GET['device']) && (strtolower($_GET['device']) == BUIWXControl::WeChat_Name || strtolower($_GET['device']) == BUIWXControl::WeXin_Name) || (isset($_GET['dv']) && strtolower($_GET['dv']) == 'wx')) {
        if ((isset($_GET['device']) && (strtolower($_GET['device']) == 'weixin' || strtolower($_GET['device']) == 'wechat')) || (isset($_GET['dv']) && strtolower($_GET['dv']) == 'wx')) {

            if (isset($_GET['sp'])) {
                $spId = $_GET['sp'];
                $spaceId = $spId;
                if ($spaceId > 0) {
                    $space = new BSpace($spaceId);
                    $appId = $space->getAttribute('wechat.appid');
                    $appSecret = $space->getAttribute('wechat.appSecret');
                    $parsedData['wxSpace'] = $space;
                    //$parsedData['wxAppId'] = $appId;
                    //$parsedData['wxAppSecret'] = $appSecret;                    
                }
            }
        }

        return $parsedData;
    }

    public function onRender($data = NULL) {
        //Blyn::app()->getUIManager()->setTheme(BUIBlyn::Theme_JMQuery_Wechat);
        parent::onRender($data);
    }

}
